/*
 * @Author: syl
 * @Date: 2023-05-18 10:16:53
 * @LastEditors: syl
 * @LastEditTime: 2023-09-26 16:15:38
 * @FilePath: \G6Doemo\g6\src\components\findCurrentNodeRelationNodes.js
 * @Description: 
 * 
 * Copyright (c) 2023 by junlin, All Rights Reserved. 
 */
class findAssociatedNodes {
    constructor() {
        this.saveSourceNodes = [];
        this.saveTargetNodes = [];
    }
    // 结束节点查找源节点
    findSourceNodes(nodeId, lines = []) {
        // 保存查找节点的源节点
        let saveSourceNodes = []//临时保存当前节点的上一级源节点
        if (lines.length != 0) {
            lines.forEach((item) => {
                if (item.target === nodeId){
                    if(!saveSourceNodes.includes(item.source))
                       saveSourceNodes.push(item.source)
                    if(!this.saveSourceNodes.includes(item.source)){
                        this.saveSourceNodes.push(item.source)
                    }
                }
                
            })
            if (saveSourceNodes.length != 0) {
                saveSourceNodes.forEach((sourceNode) => {
                    // if (!this.saveSourceNodes.includes(sourceNode))
                    //     this.saveSourceNodes.push(sourceNode)
                    this.findSourceNodes(sourceNode, lines)
                })
            }
        } 
        
        return this.saveSourceNodes
    }
    //  开始节点查找目标节点
    findTargetNodes(nodeId, lines = []) {
        let saveTargetNodes = []
        if (lines.length != 0) {
            lines.forEach((item) => {
                if (item.source === nodeId){
                    if(!saveTargetNodes.includes(item.target)){
                        saveTargetNodes.push(item.target)
                    }
                }
                   
            })
            if (saveTargetNodes.length != 0) {
                saveTargetNodes.forEach((targetNode) => {
                    if (this.saveTargetNodes.indexOf(targetNode) == -1)
                        this.saveTargetNodes.push(targetNode)
                    this.findTargetNodes(targetNode, lines)
                })
            }
        } 
        return this.saveTargetNodes
    }
}
export default findAssociatedNodes

